<?php
class Portlet{
	
	/**
	 * Get available portlets.
	 * <p>This is a static method, user can invoke via <code>Portlet::portlets()</code></p>
	 *
	 * @return array portlet array.
	 */
	public static function portlets(){
		cmspad_import('cache.DirectoryCache');
		$caches = new DirectoryCache('cmspad_portlets',CMSPAD_PORTLET_ABSPATH,DirectoryCache::$MODE_FILE,false);
		$files = array();
		foreach(array_keys($caches->value()) as $f){
			$l = strlen($f) > 5?strlen($f) - 4:0;
			if($l > 0 && substr($f,$l) == '.php'){
				$files[] = substr($f,0,$l);
			}
		}
		return $files;
	}
	
	/**
	 * Create a hyperlink from porlet and method with paramters.
	 * 
	 * @param string $portlet if it is null, using current portlet.
	 * @param string $mehtod
	 * @param array $params
	 * @return string
	 */
	public function hyperlink($portlet = null,$method = null,$params = array()){
		if(!$portlet){
			$portlet = get_class($this);
		}
		return cmspad_hyperlink($portlet,$method,$params);
	}
	
	/**
	 * Get the portlet template.
	 * <p>The portlet templates are stored in <code>CMDPAD_PORTLET_HOME/PORTLET_NAME/tpl</code> directory.</p>
	 *
	 * @param string $tpl template name.
	 * @param array $vars custom variables.
 	 * @param array $func1s user defined tag functions. empty array
 	 * @param array $func2s user defined sys functions. empty array
	 * @param bool $cache use cache for this template
	 * @return string parsed template data.
	 */
	public function template($tpl,$vars = array(),$func1s = array(),$func2s = array(), $cache = false){
		global $_SESSION;
		
		$tplPath = $this->__checkTemplate();
		$tmpPath = $this->mkTemporary('tpl');
		$tplclass = cmspad_import('Template');
		$template = new $tplclass($tplPath,$tmpPath,$cache);
		
		// Add custom variables.
		if($vars){
			foreach($vars as $varK=>$varV){
				$template->addVariable($varK,$varV);
			}
		}
		
		if(isset($_SESSION['cmspad']) && is_array($_SESSION['cmspad'])){
			foreach($_SESSION['cmspad'] as $k=>$v){
				$template->addVariable('Cmspad' . ucfirst($k),$v);
			}
		}
		
		// Add custom functions.
		if($func1s){
			foreach($func1s as $funcK=>$funcV){
				$template->addTagFunction($funcK,$funcV);
			}
		}
		
		// Add custom functions.
		if($func2s){
			foreach($func2s as $funcK=>$funcV){
				$template->addSysFunction($funcK,$funcV);
			}
		}
		
		// Set portlet const attributes.
		$template->addVariable('CMSPAD_PORTLET_NAME',get_class($this));
		$template->addVariable('CMSPAD_PORTLET_PATH',CMSPAD_PORTLET_RELPATH . get_class($this) . '/');
		
		$contents = $template->output($tpl);
		return $contents;
	}
	
	/**
	 * Check the portlet temporary path.
	 * <p>If temporary does not exist, try to create it. And if the creation is faild, return false.</p>
	 * <p>The portlet temporary path <code>CMDPAD_PORTLET_HOME/PORTLET_NAME/tmp</code>.</p>
	 *
	 * @param string $path the relative path in temporary. (e.g. "cache/data")
	 * @return string temporary file or directory absolute path.
	 */
	protected function mkTemporary($path = ''){
		if(($tmpPath = $this->__checkTempDir()) !== false){
			if($path){
				$tmpPath = $tmpPath . $path;
				if(!cmspad_create_directory($tmpPath)){
					trigger_error('Unable create portlet directory in temporary: ' . $tmpPath,E_USER_WARNING);
					return false;
				}else{
					return $tmpPath . '/';
				}
			}else{
				return $tmpPath;
			}
		}else{
			return false;
		}
	}
	
	/**
	 * Clear portlet temporary cache.
	 * <p>The portlet temporary path <code>CMDPAD_PORTLET_HOME/PORTLET_NAME/tmp</code>.</p>
	 *
	 * @param string $path temporary path.
	 */
	protected function rmTemporary($path = ''){
		$dir = CMSPAD_PORTLET_ABSPATH . get_class($this) . '/tmp' . $path;
		if(is_dir($dir)){
			// Delete system temporary files.
			cmspad_remove_directory($dir);
		}else{
			unlink($dir);
		}
	}
	
	private function __checkTemplate($extra = null){
		if(($blkPath = $this->__checkHomeDir()) !== false){
			$tplPath = $blkPath . "/tpl".($extra?'/'.$extra:'');
			if(!cmspad_create_directory($tplPath)){
				trigger_error('Unable create portlet template directory: ' . $tplPath,E_USER_WARNING);
				return false;
			}else{
				return $tplPath.($extra?'/'.$extra:'').'/';
			}
		}else{
			return false;
		}
	}
	
	private function __checkHomeDir(){
		$blkName = get_class($this);
		$blkPath = CMSPAD_PORTLET_ABSPATH . $blkName;
		if(!cmspad_create_directory($blkPath)){
			trigger_error('Unable create portlet directory: ' . $blkPath,E_USER_WARNING);
			return false;
		}else{
			return $blkPath . '/';
		}
	}
	
	private function __checkTempDir(){
		if(($blkPath = $this->__checkHomeDir()) !== false){
			$tmpPath = $blkPath . 'tmp';
			if(!cmspad_create_directory($tmpPath)){
				trigger_error('Unable create portlet temporary directory: ' . $tmpPath,E_USER_WARNING);
				return false;
			}else{
				return $tmpPath . '/';
			}
		}else{
			return false;
		}
	}
}
?>